package com.xiaohui.user.designPattern.filter;

import java.util.List;
import java.util.stream.Collectors;

public class ArticleFilterDemo {

    private List<ArticleFilterEnum> articleFilterEnumList;

    private List<IArticleFilter> iArticleFilterList;

    public ArticleFilterDemo(){
        this.init();
    }
    private void init(){
        this.iArticleFilterList = ArticleFilterFactory.createArticleFilter(articleFilterEnumList);
    }

    public void filter(List<Article> articleList){
        // 通过过滤的文章
        List<Article> articles = articleList.stream()
                .filter(e -> {
                    ArticleContext articleContext = new ArticleContext();
                    articleContext.setArticle(e);
                    return doFilter(articleContext);
                })
                .collect(Collectors.toList());
    }
    private boolean doFilter(ArticleContext articleContext){
        for (IArticleFilter articleFilter : iArticleFilterList) {
            if (!articleFilter.doFilter(articleContext)){
                return false;
            }
        }
        return true;
    }
}
